package com.xiaoxin.yixinai._frame.config;

import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.xiaoxin.yixinai._frame.base.Constant;
import com.xiaoxin.yixinai.userinfo.UserInfo;

public class LoginUser implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7679225667955985734L;
	
	private UserInfo userInfo;
	
	private Collection<? extends GrantedAuthority> authorities;
	
	/**
	 * 
	 * @param userInfo 登录用户
	 * @param authorities 菜单权限 ( 存放的是菜单的menuCode ) 
	 */
	public LoginUser(UserInfo userInfo, Collection<? extends GrantedAuthority> authorities) {
		super();
		this.userInfo = userInfo;
		this.authorities = authorities;
	}

	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Override
	public String getPassword() {
		return userInfo==null?null:userInfo.getLoginPass();
	}

	@Override
	public String getUsername() {
		return userInfo==null?null:userInfo.getLoginName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return userInfo.getUserState() == Constant.USER_STATUS_ENABLE;
	}

}
